diff options
Diffstat (limited to 'core/src/main/java/org/bouncycastle/util/test')
8 files changed, 392 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/util/test/FixedSecureRandom.java b/core/src/main/java/org/bouncycastle/util/test/FixedSecureRandom.java new file mode 100644 index 00000000..49bd7301 --- /dev/null +++ b/core/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/core/src/main/java/org/bouncycastle/util/test/NumberParsing.java b/core/src/main/java/org/bouncycastle/util/test/NumberParsing.java new file mode 100644 index 00000000..a060dd8e --- /dev/null +++ b/core/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/core/src/main/java/org/bouncycastle/util/test/SimpleTest.java b/core/src/main/java/org/bouncycastle/util/test/SimpleTest.java new file mode 100644 index 00000000..ef8ee617 --- /dev/null +++ b/core/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/core/src/main/java/org/bouncycastle/util/test/SimpleTestResult.java b/core/src/main/java/org/bouncycastle/util/test/SimpleTestResult.java new file mode 100644 index 00000000..bd69fe8e --- /dev/null +++ b/core/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/core/src/main/java/org/bouncycastle/util/test/Test.java b/core/src/main/java/org/bouncycastle/util/test/Test.java new file mode 100644 index 00000000..e631cd02 --- /dev/null +++ b/core/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/core/src/main/java/org/bouncycastle/util/test/TestFailedException.java b/core/src/main/java/org/bouncycastle/util/test/TestFailedException.java new file mode 100644 index 00000000..21e95d37 --- /dev/null +++ b/core/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/core/src/main/java/org/bouncycastle/util/test/TestResult.java b/core/src/main/java/org/bouncycastle/util/test/TestResult.java new file mode 100644 index 00000000..0a1885f0 --- /dev/null +++ b/core/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/core/src/main/java/org/bouncycastle/util/test/UncloseableOutputStream.java b/core/src/main/java/org/bouncycastle/util/test/UncloseableOutputStream.java new file mode 100644 index 00000000..89073d71 --- /dev/null +++ b/core/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); + } + } |