summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/util/test
diff options
context:
space:
mode:
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/util/test')
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/FixedSecureRandom.java135
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/NumberParsing.java34
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/SimpleTest.java84
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/SimpleTestResult.java80
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/Test.java8
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/TestFailedException.java18
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/TestResult.java10
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/UncloseableOutputStream.java23
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/test/package.html5
9 files changed, 397 insertions, 0 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/FixedSecureRandom.java b/bcprov/src/main/java/org/bouncycastle/util/test/FixedSecureRandom.java
new file mode 100644
index 0000000..49bd730
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/FixedSecureRandom.java
@@ -0,0 +1,135 @@
+package org.bouncycastle.util.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+public class FixedSecureRandom
+ extends SecureRandom
+{
+ private byte[] _data;
+
+ private int _index;
+ private int _intPad;
+
+ public FixedSecureRandom(byte[] value)
+ {
+ this(false, new byte[][] { value });
+ }
+
+ public FixedSecureRandom(
+ byte[][] values)
+ {
+ this(false, values);
+ }
+
+ /**
+ * Pad the data on integer boundaries. This is necessary for the classpath project's BigInteger
+ * implementation.
+ */
+ public FixedSecureRandom(
+ boolean intPad,
+ byte[] value)
+ {
+ this(intPad, new byte[][] { value });
+ }
+
+ /**
+ * Pad the data on integer boundaries. This is necessary for the classpath project's BigInteger
+ * implementation.
+ */
+ public FixedSecureRandom(
+ boolean intPad,
+ byte[][] values)
+ {
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ for (int i = 0; i != values.length; i++)
+ {
+ try
+ {
+ bOut.write(values[i]);
+ }
+ catch (IOException e)
+ {
+ throw new IllegalArgumentException("can't save value array.");
+ }
+ }
+
+ _data = bOut.toByteArray();
+
+ if (intPad)
+ {
+ _intPad = _data.length % 4;
+ }
+ }
+
+ public void nextBytes(byte[] bytes)
+ {
+ System.arraycopy(_data, _index, bytes, 0, bytes.length);
+
+ _index += bytes.length;
+ }
+
+ //
+ // classpath's implementation of SecureRandom doesn't currently go back to nextBytes
+ // when next is called. We can't override next as it's a final method.
+ //
+ public int nextInt()
+ {
+ int val = 0;
+
+ val |= nextValue() << 24;
+ val |= nextValue() << 16;
+
+ if (_intPad == 2)
+ {
+ _intPad--;
+ }
+ else
+ {
+ val |= nextValue() << 8;
+ }
+
+ if (_intPad == 1)
+ {
+ _intPad--;
+ }
+ else
+ {
+ val |= nextValue();
+ }
+
+ return val;
+ }
+
+ //
+ // classpath's implementation of SecureRandom doesn't currently go back to nextBytes
+ // when next is called. We can't override next as it's a final method.
+ //
+ public long nextLong()
+ {
+ long val = 0;
+
+ val |= (long)nextValue() << 56;
+ val |= (long)nextValue() << 48;
+ val |= (long)nextValue() << 40;
+ val |= (long)nextValue() << 32;
+ val |= (long)nextValue() << 24;
+ val |= (long)nextValue() << 16;
+ val |= (long)nextValue() << 8;
+ val |= (long)nextValue();
+
+ return val;
+ }
+
+ public boolean isExhausted()
+ {
+ return _index == _data.length;
+ }
+
+ private int nextValue()
+ {
+ return _data[_index++] & 0xff;
+ }
+}
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/NumberParsing.java b/bcprov/src/main/java/org/bouncycastle/util/test/NumberParsing.java
new file mode 100644
index 0000000..a060dd8
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/NumberParsing.java
@@ -0,0 +1,34 @@
+package org.bouncycastle.util.test;
+
+/**
+ * Parsing
+ */
+public final class NumberParsing
+{
+ private NumberParsing()
+ {
+ // Hide constructor
+ }
+
+ public static long decodeLongFromHex(String longAsString)
+ {
+ if ((longAsString.charAt(1) == 'x')
+ || (longAsString.charAt(1) == 'X'))
+ {
+ return Long.parseLong(longAsString.substring(2), 16);
+ }
+
+ return Long.parseLong(longAsString, 16);
+ }
+
+ public static int decodeIntFromHex(String intAsString)
+ {
+ if ((intAsString.charAt(1) == 'x')
+ || (intAsString.charAt(1) == 'X'))
+ {
+ return Integer.parseInt(intAsString.substring(2), 16);
+ }
+
+ return Integer.parseInt(intAsString, 16);
+ }
+}
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/SimpleTest.java b/bcprov/src/main/java/org/bouncycastle/util/test/SimpleTest.java
new file mode 100644
index 0000000..ef8ee61
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/SimpleTest.java
@@ -0,0 +1,84 @@
+package org.bouncycastle.util.test;
+
+import java.io.PrintStream;
+
+import org.bouncycastle.util.Arrays;
+
+public abstract class SimpleTest
+ implements Test
+{
+ public abstract String getName();
+
+ private TestResult success()
+ {
+ return SimpleTestResult.successful(this, "Okay");
+ }
+
+ protected void fail(
+ String message)
+ {
+ throw new TestFailedException(SimpleTestResult.failed(this, message));
+ }
+
+ protected void fail(
+ String message,
+ Throwable throwable)
+ {
+ throw new TestFailedException(SimpleTestResult.failed(this, message, throwable));
+ }
+
+ protected void fail(
+ String message,
+ Object expected,
+ Object found)
+ {
+ throw new TestFailedException(SimpleTestResult.failed(this, message, expected, found));
+ }
+
+ protected boolean areEqual(
+ byte[] a,
+ byte[] b)
+ {
+ return Arrays.areEqual(a, b);
+ }
+
+ public TestResult perform()
+ {
+ try
+ {
+ performTest();
+
+ return success();
+ }
+ catch (TestFailedException e)
+ {
+ return e.getResult();
+ }
+ catch (Exception e)
+ {
+ return SimpleTestResult.failed(this, "Exception: " + e, e);
+ }
+ }
+
+ protected static void runTest(
+ Test test)
+ {
+ runTest(test, System.out);
+ }
+
+ protected static void runTest(
+ Test test,
+ PrintStream out)
+ {
+ TestResult result = test.perform();
+
+ out.println(result.toString());
+ if (result.getException() != null)
+ {
+ result.getException().printStackTrace(out);
+ }
+ }
+
+ public abstract void performTest()
+ throws Exception;
+}
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/SimpleTestResult.java b/bcprov/src/main/java/org/bouncycastle/util/test/SimpleTestResult.java
new file mode 100644
index 0000000..bd69fe8
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/SimpleTestResult.java
@@ -0,0 +1,80 @@
+package org.bouncycastle.util.test;
+
+public class SimpleTestResult implements TestResult
+{
+ private static final String SEPARATOR = System.getProperty("line.separator");
+
+ private boolean success;
+ private String message;
+ private Throwable exception;
+
+ public SimpleTestResult(boolean success, String message)
+ {
+ this.success = success;
+ this.message = message;
+ }
+
+ public SimpleTestResult(boolean success, String message, Throwable exception)
+ {
+ this.success = success;
+ this.message = message;
+ this.exception = exception;
+ }
+
+ public static TestResult successful(
+ Test test,
+ String message)
+ {
+ return new SimpleTestResult(true, test.getName() + ": " + message);
+ }
+
+ public static TestResult failed(
+ Test test,
+ String message)
+ {
+ return new SimpleTestResult(false, test.getName() + ": " + message);
+ }
+
+ public static TestResult failed(
+ Test test,
+ String message,
+ Throwable t)
+ {
+ return new SimpleTestResult(false, test.getName() + ": " + message, t);
+ }
+
+ public static TestResult failed(
+ Test 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)
+ {
+ StringBuffer sb = new StringBuffer(algorithm);
+ sb.append(" failing ").append(testName);
+ sb.append(SEPARATOR).append(" expected: ").append(expected);
+ sb.append(SEPARATOR).append(" got : ").append(actual);
+
+ return sb.toString();
+ }
+
+ public boolean isSuccessful()
+ {
+ return success;
+ }
+
+ public String toString()
+ {
+ return message;
+ }
+
+ public Throwable getException()
+ {
+ return exception;
+ }
+}
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/Test.java b/bcprov/src/main/java/org/bouncycastle/util/test/Test.java
new file mode 100644
index 0000000..e631cd0
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/Test.java
@@ -0,0 +1,8 @@
+package org.bouncycastle.util.test;
+
+public interface Test
+{
+ String getName();
+
+ TestResult perform();
+}
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/TestFailedException.java b/bcprov/src/main/java/org/bouncycastle/util/test/TestFailedException.java
new file mode 100644
index 0000000..21e95d3
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/TestFailedException.java
@@ -0,0 +1,18 @@
+package org.bouncycastle.util.test;
+
+public class TestFailedException
+ extends RuntimeException
+{
+ private TestResult _result;
+
+ public TestFailedException(
+ TestResult result)
+ {
+ _result = result;
+ }
+
+ public TestResult getResult()
+ {
+ return _result;
+ }
+}
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/TestResult.java b/bcprov/src/main/java/org/bouncycastle/util/test/TestResult.java
new file mode 100644
index 0000000..0a1885f
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/TestResult.java
@@ -0,0 +1,10 @@
+package org.bouncycastle.util.test;
+
+public interface TestResult
+{
+ public boolean isSuccessful();
+
+ public Throwable getException();
+
+ public String toString();
+}
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/UncloseableOutputStream.java b/bcprov/src/main/java/org/bouncycastle/util/test/UncloseableOutputStream.java
new file mode 100644
index 0000000..89073d7
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/UncloseableOutputStream.java
@@ -0,0 +1,23 @@
+package org.bouncycastle.util.test;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class UncloseableOutputStream extends FilterOutputStream
+{
+ public UncloseableOutputStream(OutputStream s)
+ {
+ super(s);
+ }
+
+ public void close()
+ {
+ throw new RuntimeException("close() called on UncloseableOutputStream");
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ out.write(b, off, len);
+ }
+ }
diff --git a/bcprov/src/main/java/org/bouncycastle/util/test/package.html b/bcprov/src/main/java/org/bouncycastle/util/test/package.html
new file mode 100644
index 0000000..e723fd1
--- /dev/null
+++ b/bcprov/src/main/java/org/bouncycastle/util/test/package.html
@@ -0,0 +1,5 @@
+<html>
+<body bgcolor="#ffffff">
+Light weight test API. If you can use Junit!
+</body>
+</html>