Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/test/java/org/spongycastle/util')
-rw-r--r--core/src/test/java/org/spongycastle/util/encoders/test/AbstractCoderTest.java211
-rw-r--r--core/src/test/java/org/spongycastle/util/encoders/test/AllTests.java20
-rw-r--r--core/src/test/java/org/spongycastle/util/encoders/test/Base64Test.java121
-rw-r--r--core/src/test/java/org/spongycastle/util/encoders/test/EncoderTest.java250
-rw-r--r--core/src/test/java/org/spongycastle/util/encoders/test/HexTest.java92
-rw-r--r--core/src/test/java/org/spongycastle/util/encoders/test/UrlBase64Test.java119
-rw-r--r--core/src/test/java/org/spongycastle/util/io/pem/test/AllTests.java71
-rw-r--r--core/src/test/java/org/spongycastle/util/io/test/BufferingOutputStreamTest.java68
-rw-r--r--core/src/test/java/org/spongycastle/util/utiltest/AllTests.java20
-rw-r--r--core/src/test/java/org/spongycastle/util/utiltest/BigIntegersTest.java90
-rw-r--r--core/src/test/java/org/spongycastle/util/utiltest/IPTest.java55
11 files changed, 1117 insertions, 0 deletions
diff --git a/core/src/test/java/org/spongycastle/util/encoders/test/AbstractCoderTest.java b/core/src/test/java/org/spongycastle/util/encoders/test/AbstractCoderTest.java
new file mode 100644
index 00000000..03f0bf77
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/encoders/test/AbstractCoderTest.java
@@ -0,0 +1,211 @@
+package org.spongycastle.util.encoders.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.spongycastle.util.encoders.Encoder;
+
+public abstract class AbstractCoderTest extends TestCase
+{
+
+ private static final int[] SIZES_TO_CHECK = {64, 128, 1024, 1025, 1026, 2048,
+ 2049, 2050, 4096, 4097, 4098, 8192, 8193, 8194};
+
+ protected Encoder enc;
+ private Random r;
+
+ AbstractCoderTest(
+ String name)
+ {
+ super(name);
+ }
+
+ protected void setUp()
+ {
+ r = new Random();
+ }
+
+ private void checkArrayOfSize(int size)
+ throws IOException
+ {
+ byte[] original = new byte[size];
+ r.nextBytes(original);
+
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+ enc.encode(original, 0, original.length, bOut);
+
+ byte[] encoded = bOut.toByteArray();
+
+ assertTrue(encoded.length > original.length);
+ assertTrue(encoded.length <= (original.length * 2));
+ checkEncoding(encoded);
+ checkSimpleDecode(original, encoded);
+ checkStringDecode(original, encoded);
+ checkOutputStreamDecode(original, encoded);
+
+ int offset = r.nextInt(20);
+ byte[] offsetEncoded = new byte[offset + encoded.length];
+ System.arraycopy(encoded, 0, offsetEncoded, offset, encoded.length);
+ checkOffsetDecode(original, offsetEncoded, offset, encoded.length);
+
+ offset = r.nextInt(20);
+ byte[] offsetOriginal = new byte[offset + original.length];
+ System.arraycopy(original, 0, offsetOriginal, offset, original.length);
+ checkOffsetEncode(original, offsetOriginal, offset, original.length);
+
+ byte[] encodedWithSpace = addWhitespace(encoded);
+ checkSimpleDecode(original, encodedWithSpace);
+ checkStringDecode(original, encodedWithSpace);
+ checkOutputStreamDecode(original, encodedWithSpace);
+ }
+
+ public void testEncode()
+ throws IOException
+ {
+ for (int i = 0; i < SIZES_TO_CHECK.length; i++)
+ {
+ checkArrayOfSize(SIZES_TO_CHECK[i]);
+ }
+ }
+
+ private void checkEncoding(byte[] encoded)
+ {
+ String encString = convertBytesToString(encoded);
+ for (int i = 0; i < encString.length(); i++)
+ {
+ char c = encString.charAt(i);
+ if (c == paddingChar())
+ {
+ // should only be padding at end of string
+ assertTrue(i > encString.length() - 3);
+ continue;
+ }
+ else if (isEncodedChar(c))
+ {
+ continue;
+ }
+ fail("Unexpected encoded character " + c);
+ }
+ }
+
+ private void checkOutputStreamDecode(byte[] original, byte[] encoded)
+ {
+ String encString = convertBytesToString(encoded);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try
+ {
+ assertEquals(original.length, enc.decode(encString, out));
+ assertTrue(Arrays.equals(original, out.toByteArray()));
+ }
+ catch (IOException e)
+ {
+ fail("This shouldn't happen");
+ }
+ }
+
+ private void checkSimpleDecode(byte[] original, byte[] encoded)
+ throws IOException
+ {
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+ enc.decode(encoded, 0, encoded.length, bOut);
+
+ assertTrue(Arrays.equals(original, bOut.toByteArray()));
+ }
+
+ private void checkOffsetEncode(byte[] original, byte[] offsetOriginal, int off, int length)
+ throws IOException
+ {
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ enc.encode(offsetOriginal, off, length, bOut);
+
+ byte[] encoded = bOut.toByteArray();
+
+ bOut.reset();
+
+ enc.decode(encoded, 0, encoded.length, bOut);
+
+ assertTrue(Arrays.equals(original, bOut.toByteArray()));
+ }
+
+ private void checkOffsetDecode(byte[] original, byte[] encoded, int off, int length)
+ throws IOException
+ {
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+ enc.decode(encoded, off, length, bOut);
+
+ assertTrue(Arrays.equals(original, bOut.toByteArray()));
+ }
+
+ private void checkStringDecode(byte[] original, byte[] encoded)
+ throws IOException
+ {
+ String encString = convertBytesToString(encoded);
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+ enc.decode(encString, bOut);
+ assertTrue(Arrays.equals(original, bOut.toByteArray()));
+ }
+
+ private byte[] addWhitespace(byte[] encoded)
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ addSpace(out);
+ for (int i = 0; i < encoded.length - 5; i++)
+ {
+ out.write(encoded, i, 1);
+ if (r.nextInt(100) < 5)
+ {
+ addSpace(out);
+ }
+ }
+ for (int i = encoded.length - 5; i < encoded.length; i++)
+ {
+ out.write(encoded, i, 1);
+ }
+ addSpace(out);
+ return out.toByteArray();
+ }
+
+ private void addSpace(ByteArrayOutputStream out)
+ {
+ do
+ {
+ switch (r.nextInt(3))
+ {
+ case 0 :
+ out.write((int) '\n');
+ break;
+ case 1 :
+ out.write((int) '\r');
+ break;
+ case 2 :
+ out.write((int) '\t');
+ break;
+ case 3 :
+ out.write((int) ' ');
+ break;
+ }
+ } while (r.nextBoolean());
+ }
+
+ private String convertBytesToString(byte[] encoded)
+ {
+ StringBuffer b = new StringBuffer();
+
+ for (int i = 0; i != encoded.length; i++)
+ {
+ b.append((char)(encoded[i] & 0xff));
+ }
+
+ return b.toString();
+ }
+
+ abstract protected char paddingChar();
+
+ abstract protected boolean isEncodedChar(char c);
+
+}
diff --git a/core/src/test/java/org/spongycastle/util/encoders/test/AllTests.java b/core/src/test/java/org/spongycastle/util/encoders/test/AllTests.java
new file mode 100644
index 00000000..a31c5ad2
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/encoders/test/AllTests.java
@@ -0,0 +1,20 @@
+package org.spongycastle.util.encoders.test;
+
+import junit.framework.*;
+
+public class AllTests
+{
+ public static void main (String[] args)
+ {
+ junit.textui.TestRunner.run (suite());
+ }
+
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("encoder tests");
+ suite.addTestSuite(Base64Test.class);
+ suite.addTestSuite(UrlBase64Test.class);
+ suite.addTestSuite(HexTest.class);
+ return suite;
+ }
+}
diff --git a/core/src/test/java/org/spongycastle/util/encoders/test/Base64Test.java b/core/src/test/java/org/spongycastle/util/encoders/test/Base64Test.java
new file mode 100644
index 00000000..f74f7eed
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/encoders/test/Base64Test.java
@@ -0,0 +1,121 @@
+package org.spongycastle.util.encoders.test;
+
+import java.io.IOException;
+
+import org.spongycastle.util.Arrays;
+import org.spongycastle.util.Strings;
+import org.spongycastle.util.encoders.Base64;
+import org.spongycastle.util.encoders.Base64Encoder;
+import org.spongycastle.util.encoders.DecoderException;
+import org.spongycastle.util.encoders.Hex;
+
+public class Base64Test extends AbstractCoderTest
+{
+ private static final String sample1 = "mO4TyLWG7vjFWdKT8IJcVbZ/jwc=";
+ private static final byte[] sample1Bytes = Hex.decode("98ee13c8b586eef8c559d293f0825c55b67f8f07");
+ private static final String sample2 = "F4I4p8Vf/mS+Kxvri3FPoMcqmJ1f";
+ private static final byte[] sample2Bytes = Hex.decode("178238a7c55ffe64be2b1beb8b714fa0c72a989d5f");
+ private static final String sample3 = "UJmEdJYodqHJmd7Rtv6/OP29/jUEFw==";
+ private static final byte[] sample3Bytes = Hex.decode("50998474962876a1c999ded1b6febf38fdbdfe350417");
+
+ private static final String invalid1 = "%O4TyLWG7vjFWdKT8IJcVbZ/jwc=";
+ private static final String invalid2 = "F%I4p8Vf/mS+Kxvri3FPoMcqmJ1f";
+ private static final String invalid3 = "UJ%EdJYodqHJmd7Rtv6/OP29/jUEFw==";
+ private static final String invalid4 = "mO4%yLWG7vjFWdKT8IJcVbZ/jwc=";
+ private static final String invalid5 = "UJmEdJYodqHJmd7Rtv6/OP29/jUEF%==";
+ private static final String invalid6 = "mO4TyLWG7vjFWdKT8IJcVbZ/jw%=";
+ private static final String invalid7 = "F4I4p8Vf/mS+Kxvri3FPoMcqmJ1%";
+ private static final String invalid8 = "UJmEdJYodqHJmd7Rtv6/OP29/jUE%c==";
+ private static final String invalid9 = "mO4TyLWG7vjFWdKT8IJcVbZ/j%c=";
+ private static final String invalida = "F4I4p8Vf/mS+Kxvri3FPoMcqmJ%1";
+ private static final String invalidb = "UJmEdJYodqHJmd7Rtv6/OP29/jU%Fc==";
+ private static final String invalidc = "mO4TyLWG7vjFWdKT8IJcVbZ/%wc=";
+ private static final String invalidd = "F4I4p8Vf/mS+Kxvri3FPoMcqm%1c";
+
+
+ public Base64Test(
+ String name)
+ {
+ super(name);
+ }
+
+ protected void setUp()
+ {
+ super.setUp();
+ enc = new Base64Encoder();
+ }
+
+ public void testSamples()
+ throws IOException
+ {
+ assertTrue(Arrays.areEqual(sample1Bytes, Base64.decode(sample1)));
+ assertTrue(Arrays.areEqual(sample1Bytes, Base64.decode(Strings.toByteArray(sample1))));
+ assertTrue(Arrays.areEqual(sample2Bytes, Base64.decode(sample2)));
+ assertTrue(Arrays.areEqual(sample2Bytes, Base64.decode(Strings.toByteArray(sample2))));
+ assertTrue(Arrays.areEqual(sample3Bytes, Base64.decode(sample3)));
+ assertTrue(Arrays.areEqual(sample3Bytes, Base64.decode(Strings.toByteArray(sample3))));
+ }
+
+ public void testInvalidInput()
+ throws IOException
+ {
+ String[] invalid = new String[] { invalid1, invalid2, invalid3, invalid4, invalid5, invalid6, invalid7, invalid8, invalid9, invalida, invalidb, invalidc, invalidd };
+
+ for (int i = 0; i != invalid.length; i++)
+ {
+ invalidTest(invalid[i]);
+ invalidTest(Strings.toByteArray(invalid[i]));
+ }
+ }
+
+ private void invalidTest(String data)
+ {
+ try
+ {
+ Base64.decode(data);
+ }
+ catch (DecoderException e)
+ {
+ return;
+ }
+
+ fail("invalid String data parsed");
+ }
+
+ private void invalidTest(byte[] data)
+ {
+ try
+ {
+ Base64.decode(data);
+ }
+ catch (DecoderException e)
+ {
+ return;
+ }
+
+ fail("invalid byte data parsed");
+ }
+
+ protected char paddingChar()
+ {
+ return '=';
+ }
+
+ protected boolean isEncodedChar(char c)
+ {
+ if (Character.isLetterOrDigit(c))
+ {
+ return true;
+ }
+ else if (c == '+')
+ {
+ return true;
+ }
+ else if (c == '/')
+ {
+ return true;
+ }
+ return false;
+ }
+
+}
diff --git a/core/src/test/java/org/spongycastle/util/encoders/test/EncoderTest.java b/core/src/test/java/org/spongycastle/util/encoders/test/EncoderTest.java
new file mode 100644
index 00000000..4e0e79c2
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/encoders/test/EncoderTest.java
@@ -0,0 +1,250 @@
+package org.spongycastle.util.encoders.test;
+
+import java.util.Arrays;
+import java.util.Random;
+
+import org.spongycastle.util.encoders.Base64;
+import org.spongycastle.util.encoders.Hex;
+import org.spongycastle.util.test.SimpleTest;
+
+public class EncoderTest
+ extends SimpleTest
+{
+ public static final boolean DEBUG = true;
+
+
+ public static void main(
+ String[] args)
+ {
+ runTest(new EncoderTest());
+ }
+
+ public String getName()
+ {
+ return "Encoder";
+ }
+
+ /*
+ *
+ * TESTS
+ *
+ */
+
+ public void performTest()
+ {
+ testHex();
+ testBase64();
+ testBase64WithNL();
+ }
+
+
+ public void testBase64()
+ {
+ try
+ {
+ Random _r = new Random();
+
+ byte[] _orig1024 = new byte[1024];
+ _r.nextBytes(_orig1024);
+
+ byte[] _orig2048 = new byte[2048];
+ _r.nextBytes(_orig2048);
+
+ byte[] _orig4096 = new byte[4096];
+ _r.nextBytes(_orig4096);
+
+ byte[] _orig8192 = new byte[8192];
+ _r.nextBytes(_orig8192);
+
+ byte[] _enc1024 = Base64.encode(_orig1024);
+ byte[] _enc2048 = Base64.encode(_orig2048);
+ byte[] _enc4096 = Base64.encode(_orig4096);
+ byte[] _enc8192 = Base64.encode(_orig8192);
+
+ byte[] _dec1024 = Base64.decode(_enc1024);
+ byte[] _dec2048 = Base64.decode(_enc2048);
+ byte[] _dec4096 = Base64.decode(_enc4096);
+ byte[] _dec8192 = Base64.decode(_enc8192);
+
+ if(!Arrays.equals(_orig1024, _dec1024))
+ {
+ fail("Failed Base64 test");
+ }
+
+ if(!Arrays.equals(_orig2048, _dec2048))
+ {
+ fail("Failed Base64 test");
+ }
+
+ if(!Arrays.equals(_orig4096, _dec4096))
+ {
+ fail("Failed Base64 test");
+ }
+
+ if(!Arrays.equals(_orig8192, _dec8192))
+ {
+ fail("Failed Base64 test");
+ }
+
+
+
+ byte[] _orig1025 = new byte[1025];
+ _r.nextBytes(_orig1025);
+
+ byte[] _orig2049 = new byte[2049];
+ _r.nextBytes(_orig2049);
+
+ byte[] _orig4097 = new byte[4097];
+ _r.nextBytes(_orig4097);
+
+ byte[] _orig8193 = new byte[8193];
+ _r.nextBytes(_orig8193);
+
+ byte[] _enc1025 = Base64.encode(_orig1025);
+ byte[] _enc2049 = Base64.encode(_orig2049);
+ byte[] _enc4097 = Base64.encode(_orig4097);
+ byte[] _enc8193 = Base64.encode(_orig8193);
+
+ byte[] _dec1025 = Base64.decode(_enc1025);
+ byte[] _dec2049 = Base64.decode(_enc2049);
+ byte[] _dec4097 = Base64.decode(_enc4097);
+ byte[] _dec8193 = Base64.decode(_enc8193);
+
+ if(!Arrays.equals(_orig1025, _dec1025))
+ {
+ fail("Failed Base64 test");
+ }
+
+ if(!Arrays.equals(_orig2049, _dec2049))
+ {
+ fail("Failed Base64 test");
+ }
+
+ if(!Arrays.equals(_orig4097, _dec4097))
+ {
+ fail("Failed Base64 test");
+ }
+
+ if(!Arrays.equals(_orig8193, _dec8193))
+ {
+ fail("Failed Base64 test");
+ }
+ }
+ catch(Exception ex)
+ {
+ fail("Failed Base64 test");
+ }
+ }
+
+ public void testBase64WithNL()
+ {
+ byte[] dec = Base64.decode("SVNC" + "\n" + "QUQ=\n");
+
+ if (dec.length != 5)
+ {
+ fail("got length " + dec.length + " when expecting 10");
+ }
+
+ if (!areEqual(dec, Base64.decode("SVNCQUQ=")))
+ {
+ fail("decodings are not equal");
+ }
+ }
+
+ public void testHex()
+ {
+ try
+ {
+ Random _r = new Random();
+
+ byte[] _orig1024 = new byte[1024];
+ _r.nextBytes(_orig1024);
+
+ byte[] _orig2048 = new byte[2048];
+ _r.nextBytes(_orig2048);
+
+ byte[] _orig4096 = new byte[4096];
+ _r.nextBytes(_orig4096);
+
+ byte[] _orig8192 = new byte[8192];
+ _r.nextBytes(_orig8192);
+
+ byte[] _enc1024 = Hex.encode(_orig1024);
+ byte[] _enc2048 = Hex.encode(_orig2048);
+ byte[] _enc4096 = Hex.encode(_orig4096);
+ byte[] _enc8192 = Hex.encode(_orig8192);
+
+ byte[] _dec1024 = Hex.decode(_enc1024);
+ byte[] _dec2048 = Hex.decode(_enc2048);
+ byte[] _dec4096 = Hex.decode(_enc4096);
+ byte[] _dec8192 = Hex.decode(_enc8192);
+
+ if(!Arrays.equals(_orig1024, _dec1024))
+ {
+ fail("Failed Hex test");
+ }
+
+ if(!Arrays.equals(_orig2048, _dec2048))
+ {
+ fail("Failed Hex test");
+ }
+
+ if(!Arrays.equals(_orig4096, _dec4096))
+ {
+ fail("Failed Hex test");
+ }
+
+ if(!Arrays.equals(_orig8192, _dec8192))
+ {
+ fail("Failed Hex test");
+ }
+
+
+ byte[] _orig1025 = new byte[1025];
+ _r.nextBytes(_orig1025);
+
+ byte[] _orig2049 = new byte[2049];
+ _r.nextBytes(_orig2049);
+
+ byte[] _orig4097 = new byte[4097];
+ _r.nextBytes(_orig4097);
+
+ byte[] _orig8193 = new byte[8193];
+ _r.nextBytes(_orig8193);
+
+ byte[] _enc1025 = Hex.encode(_orig1025);
+ byte[] _enc2049 = Hex.encode(_orig2049);
+ byte[] _enc4097 = Hex.encode(_orig4097);
+ byte[] _enc8193 = Hex.encode(_orig8193);
+
+ byte[] _dec1025 = Hex.decode(_enc1025);
+ byte[] _dec2049 = Hex.decode(_enc2049);
+ byte[] _dec4097 = Hex.decode(_enc4097);
+ byte[] _dec8193 = Hex.decode(_enc8193);
+
+ if(!Arrays.equals(_orig1025, _dec1025))
+ {
+ fail("Failed Hex test");
+ }
+
+ if(!Arrays.equals(_orig2049, _dec2049))
+ {
+ fail("Failed Hex test");
+ }
+
+ if(!Arrays.equals(_orig4097, _dec4097))
+ {
+ fail("Failed Hex test");
+ }
+
+ if(!Arrays.equals(_orig8193, _dec8193))
+ {
+ fail("Failed Hex test");
+ }
+ }
+ catch(Exception ex)
+ {
+ fail("Failed Hex test");
+ }
+ }
+}
diff --git a/core/src/test/java/org/spongycastle/util/encoders/test/HexTest.java b/core/src/test/java/org/spongycastle/util/encoders/test/HexTest.java
new file mode 100644
index 00000000..a0c101bc
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/encoders/test/HexTest.java
@@ -0,0 +1,92 @@
+package org.spongycastle.util.encoders.test;
+
+import java.io.IOException;
+
+import org.spongycastle.util.Strings;
+import org.spongycastle.util.encoders.DecoderException;
+import org.spongycastle.util.encoders.Hex;
+import org.spongycastle.util.encoders.HexEncoder;
+
+public class HexTest extends AbstractCoderTest
+{
+ private static final String invalid1 = "%O4T";
+ private static final String invalid2 = "FZI4";
+ private static final String invalid3 = "ae%E";
+ private static final String invalid4 = "fO4%";
+ private static final String invalid5 = "beefe";
+ private static final String invalid6 = "beefs";
+
+ public HexTest(
+ String name)
+ {
+ super(name);
+ }
+
+ protected void setUp()
+ {
+ super.setUp();
+ enc = new HexEncoder();
+ }
+
+ protected char paddingChar()
+ {
+ return 0;
+ }
+
+ protected boolean isEncodedChar(char c)
+ {
+ if ('A' <= c && c <= 'F')
+ {
+ return true;
+ }
+ if ('a' <= c && c <= 'f')
+ {
+ return true;
+ }
+ if ('0' <= c && c <= '9')
+ {
+ return true;
+ }
+ return false;
+ }
+
+ public void testInvalidInput()
+ throws IOException
+ {
+ String[] invalid = new String[] { invalid1, invalid2, invalid3, invalid4, invalid5, invalid6 };
+
+ for (int i = 0; i != invalid.length; i++)
+ {
+ invalidTest(invalid[i]);
+ invalidTest(Strings.toByteArray(invalid[i]));
+ }
+ }
+
+ private void invalidTest(String data)
+ {
+ try
+ {
+ Hex.decode(data);
+ }
+ catch (DecoderException e)
+ {
+ return;
+ }
+
+ fail("invalid String data parsed");
+ }
+
+ private void invalidTest(byte[] data)
+ {
+ try
+ {
+ Hex.decode(data);
+ }
+ catch (DecoderException e)
+ {
+ return;
+ }
+
+ fail("invalid byte data parsed");
+ }
+}
diff --git a/core/src/test/java/org/spongycastle/util/encoders/test/UrlBase64Test.java b/core/src/test/java/org/spongycastle/util/encoders/test/UrlBase64Test.java
new file mode 100644
index 00000000..9822efea
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/encoders/test/UrlBase64Test.java
@@ -0,0 +1,119 @@
+package org.spongycastle.util.encoders.test;
+
+import java.io.IOException;
+
+import org.spongycastle.util.Arrays;
+import org.spongycastle.util.Strings;
+import org.spongycastle.util.encoders.DecoderException;
+import org.spongycastle.util.encoders.Hex;
+import org.spongycastle.util.encoders.UrlBase64;
+import org.spongycastle.util.encoders.UrlBase64Encoder;
+
+public class UrlBase64Test extends AbstractCoderTest
+{
+ private static final String sample1 = "mO4TyLWG7vjFWdKT8IJcVbZ_jwc.";
+ private static final byte[] sample1Bytes = Hex.decode("98ee13c8b586eef8c559d293f0825c55b67f8f07");
+ private static final String sample2 = "F4I4p8Vf_mS-Kxvri3FPoMcqmJ1f";
+ private static final byte[] sample2Bytes = Hex.decode("178238a7c55ffe64be2b1beb8b714fa0c72a989d5f");
+ private static final String sample3 = "UJmEdJYodqHJmd7Rtv6_OP29_jUEFw..";
+ private static final byte[] sample3Bytes = Hex.decode("50998474962876a1c999ded1b6febf38fdbdfe350417");
+
+ private static final String invalid1 = "%O4TyLWG7vjFWdKT8IJcVbZ_jwc.";
+ private static final String invalid2 = "F%I4p8Vf_mS-Kxvri3FPoMcqmJ1f";
+ private static final String invalid3 = "UJ%EdJYodqHJmd7Rtv6_OP29_jUEFw..";
+ private static final String invalid4 = "mO4%yLWG7vjFWdKT8IJcVbZ_jwc.";
+ private static final String invalid5 = "UJmEdJYodqHJmd7Rtv6_OP29_jUEF%..";
+ private static final String invalid6 = "mO4TyLWG7vjFWdKT8IJcVbZ_jw%.";
+ private static final String invalid7 = "F4I4p8Vf_mS-Kxvri3FPoMcqmJ1%";
+ private static final String invalid8 = "UJmEdJYodqHJmd7Rtv6_OP29_jUE%c..";
+ private static final String invalid9 = "mO4TyLWG7vjFWdKT8IJcVbZ_j%c.";
+ private static final String invalida = "F4I4p8Vf_mS-Kxvri3FPoMcqmJ%1";
+ private static final String invalidb = "UJmEdJYodqHJmd7Rtv6_OP29_jU%Fc..";
+ private static final String invalidc = "mO4TyLWG7vjFWdKT8IJcVbZ_%wc.";
+ private static final String invalidd = "F4I4p8Vf_mS-Kxvri3FPoMcqm%1c";
+
+ public UrlBase64Test(
+ String name)
+ {
+ super(name);
+ }
+
+ protected void setUp()
+ {
+ super.setUp();
+ enc = new UrlBase64Encoder();
+ }
+
+ public void testSamples()
+ throws IOException
+ {
+ assertTrue(Arrays.areEqual(sample1Bytes, UrlBase64.decode(sample1)));
+ assertTrue(Arrays.areEqual(sample1Bytes, UrlBase64.decode(Strings.toByteArray(sample1))));
+ assertTrue(Arrays.areEqual(sample2Bytes, UrlBase64.decode(sample2)));
+ assertTrue(Arrays.areEqual(sample2Bytes, UrlBase64.decode(Strings.toByteArray(sample2))));
+ assertTrue(Arrays.areEqual(sample3Bytes, UrlBase64.decode(sample3)));
+ assertTrue(Arrays.areEqual(sample3Bytes, UrlBase64.decode(Strings.toByteArray(sample3))));
+ }
+
+ public void testInvalidInput()
+ throws IOException
+ {
+ String[] invalid = new String[] { invalid1, invalid2, invalid3, invalid4, invalid5, invalid6, invalid7, invalid8, invalid9, invalida, invalidb, invalidc, invalidd };
+
+ for (int i = 0; i != invalid.length; i++)
+ {
+ invalidTest(invalid[i]);
+ invalidTest(Strings.toByteArray(invalid[i]));
+ }
+ }
+
+ private void invalidTest(String data)
+ {
+ try
+ {
+ UrlBase64.decode(data);
+ }
+ catch (DecoderException e)
+ {
+ return;
+ }
+
+ fail("invalid String data parsed");
+ }
+
+ private void invalidTest(byte[] data)
+ {
+ try
+ {
+ UrlBase64.decode(data);
+ }
+ catch (DecoderException e)
+ {
+ return;
+ }
+
+ fail("invalid byte data parsed");
+ }
+
+ protected char paddingChar()
+ {
+ return '.';
+ }
+
+ protected boolean isEncodedChar(char c)
+ {
+ if (Character.isLetterOrDigit(c))
+ {
+ return true;
+ }
+ else if (c == '-')
+ {
+ return true;
+ }
+ else if (c == '_')
+ {
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/core/src/test/java/org/spongycastle/util/io/pem/test/AllTests.java b/core/src/test/java/org/spongycastle/util/io/pem/test/AllTests.java
new file mode 100644
index 00000000..6c04ace6
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/io/pem/test/AllTests.java
@@ -0,0 +1,71 @@
+package org.spongycastle.util.io.pem.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.spongycastle.util.io.pem.PemHeader;
+import org.spongycastle.util.io.pem.PemObject;
+import org.spongycastle.util.io.pem.PemWriter;
+
+public class AllTests
+ extends TestCase
+{
+ public void testPemLength()
+ throws IOException
+ {
+ for (int i = 1; i != 60; i++)
+ {
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[i]);
+ }
+
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[100]);
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[101]);
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[102]);
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[103]);
+
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[1000]);
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[1001]);
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[1002]);
+ lengthTest("CERTIFICATE", Collections.EMPTY_LIST, new byte[1003]);
+
+ List 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, List headers, byte[] data)
+ throws IOException
+ {
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+ PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut));
+
+ PemObject pemObj = new PemObject(type, headers, data);
+ pWrt.writeObject(pemObj);
+
+ pWrt.close();
+
+ assertEquals(bOut.toByteArray().length, pWrt.getOutputSize(pemObj));
+ }
+
+ public static void main (String[] args)
+ {
+ junit.textui.TestRunner.run (suite());
+ }
+
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("util tests");
+ suite.addTestSuite(AllTests.class);
+ return suite;
+ }
+} \ No newline at end of file
diff --git a/core/src/test/java/org/spongycastle/util/io/test/BufferingOutputStreamTest.java b/core/src/test/java/org/spongycastle/util/io/test/BufferingOutputStreamTest.java
new file mode 100644
index 00000000..faaaa532
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/io/test/BufferingOutputStreamTest.java
@@ -0,0 +1,68 @@
+package org.spongycastle.util.io.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+import org.spongycastle.util.io.BufferingOutputStream;
+import org.spongycastle.util.test.SimpleTest;
+
+public class BufferingOutputStreamTest
+ extends SimpleTest
+{
+ public String getName()
+ {
+ return "BufferingStreamTest";
+ }
+
+ public void performTest()
+ throws Exception
+ {
+ SecureRandom random = new SecureRandom();
+
+ for (int i = 1; i != 256; i++)
+ {
+ byte[] data = new byte[i];
+
+ random.nextBytes(data);
+
+ checkStream(data, 16);
+ checkStream(data, 33);
+ checkStream(data, 128);
+ }
+ }
+
+ private void checkStream(byte[] data, int bufsize)
+ throws IOException
+ {
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+ BufferingOutputStream bfOut = new BufferingOutputStream(bOut, bufsize);
+
+ for (int i = 0; i != 10; i++)
+ {
+ bfOut.write(data[0]);
+ bfOut.write(data, 1, data.length - 1);
+ }
+
+ bfOut.close();
+
+ byte[] output = bOut.toByteArray();
+
+ for (int i = 0; i != 10; i++)
+ {
+ for (int j = 0; j != data.length; j++)
+ {
+ if (output[i * data.length + j] != data[j])
+ {
+ fail("data mismatch!");
+ }
+ }
+ }
+ }
+
+ public static void main(
+ String[] args)
+ {
+ runTest(new BufferingOutputStreamTest());
+ }
+}
diff --git a/core/src/test/java/org/spongycastle/util/utiltest/AllTests.java b/core/src/test/java/org/spongycastle/util/utiltest/AllTests.java
new file mode 100644
index 00000000..37c73eb8
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/utiltest/AllTests.java
@@ -0,0 +1,20 @@
+package org.spongycastle.util.utiltest;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class AllTests
+{
+ public static void main (String[] args)
+ {
+ junit.textui.TestRunner.run (suite());
+ }
+
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("util tests");
+ suite.addTestSuite(IPTest.class);
+ suite.addTestSuite(BigIntegersTest.class);
+ return suite;
+ }
+}
diff --git a/core/src/test/java/org/spongycastle/util/utiltest/BigIntegersTest.java b/core/src/test/java/org/spongycastle/util/utiltest/BigIntegersTest.java
new file mode 100644
index 00000000..f17745ee
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/utiltest/BigIntegersTest.java
@@ -0,0 +1,90 @@
+package org.spongycastle.util.utiltest;
+
+import java.math.BigInteger;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+import org.spongycastle.util.Arrays;
+import org.spongycastle.util.BigIntegers;
+import org.spongycastle.util.IPAddress;
+import org.spongycastle.util.encoders.Hex;
+
+public class BigIntegersTest
+ extends TestCase
+{
+ public String getName()
+ {
+ return "BigIntegers";
+ }
+
+ public void testAsUnsignedByteArray()
+ {
+ BigInteger a = new BigInteger(1, Hex.decode("ff12345678"));
+
+ byte[] a5 = BigIntegers.asUnsignedByteArray(a);
+
+ Assert.assertEquals(5, a5.length);
+ Assert.assertTrue(Arrays.areEqual(a5, Hex.decode("ff12345678")));
+
+ BigInteger b = new BigInteger(1, Hex.decode("0f12345678"));
+
+ byte[] b5 = BigIntegers.asUnsignedByteArray(b);
+
+ Assert.assertEquals(5, b5.length);
+ Assert.assertTrue(Arrays.areEqual(b5, Hex.decode("0f12345678")));
+ }
+
+ public void testFixedLengthUnsignedByteArray()
+ {
+ BigInteger a = new BigInteger(1, Hex.decode("ff12345678"));
+
+ byte[] a5 = BigIntegers.asUnsignedByteArray(5, a);
+
+ Assert.assertEquals(5, a5.length);
+ Assert.assertTrue(Arrays.areEqual(a5, Hex.decode("ff12345678")));
+
+ byte[] a6 = BigIntegers.asUnsignedByteArray(6, a);
+
+ Assert.assertEquals(6, a6.length);
+ Assert.assertEquals(0, a6[0]);
+ Assert.assertTrue(Arrays.areEqual(a6, Hex.decode("00ff12345678")));
+
+ BigInteger b = new BigInteger(1, Hex.decode("0f12345678"));
+
+ byte[] b5 = BigIntegers.asUnsignedByteArray(5, b);
+
+ Assert.assertEquals(5, b5.length);
+ Assert.assertTrue(Arrays.areEqual(b5, Hex.decode("0f12345678")));
+
+ byte[] b6 = BigIntegers.asUnsignedByteArray(6, b);
+
+ Assert.assertEquals(6, b6.length);
+ Assert.assertEquals(0, b6[0]);
+ Assert.assertTrue(Arrays.areEqual(b6, Hex.decode("000f12345678")));
+
+ BigInteger c = new BigInteger(1, Hex.decode("ff123456789a"));
+
+ try
+ {
+ byte[] c5 = BigIntegers.asUnsignedByteArray(5, c);
+
+ fail("no exception thrown");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // ignore
+ }
+
+ BigInteger d = new BigInteger(1, Hex.decode("0f123456789a"));
+ try
+ {
+ byte[] c5 = BigIntegers.asUnsignedByteArray(5, d);
+
+ fail("no exception thrown");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // ignore
+ }
+ }
+}
diff --git a/core/src/test/java/org/spongycastle/util/utiltest/IPTest.java b/core/src/test/java/org/spongycastle/util/utiltest/IPTest.java
new file mode 100644
index 00000000..93df95b9
--- /dev/null
+++ b/core/src/test/java/org/spongycastle/util/utiltest/IPTest.java
@@ -0,0 +1,55 @@
+package org.spongycastle.util.utiltest;
+
+import junit.framework.TestCase;
+import org.spongycastle.util.IPAddress;
+
+public class IPTest
+ extends TestCase
+{
+
+ private static final String validIP4v[] = new String[]
+ { "0.0.0.0", "255.255.255.255", "192.168.0.0" };
+
+ private static final String invalidIP4v[] = new String[]
+ { "0.0.0.0.1", "256.255.255.255", "1", "A.B.C", "1:.4.6.5" };
+
+ private static final 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 final String invalidIP6v[] = new String[]
+ { "0.0.0.0:1", "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFFF" };
+
+ private void testIP(String[] valid, String[] invalid)
+ {
+ for (int i = 0; i < valid.length; i++)
+ {
+ if (!IPAddress.isValid(valid[i]))
+ {
+ fail("Valid input string not accepted: " + valid[i] + ".");
+ }
+ }
+ for (int i = 0; i < invalid.length; i++)
+ {
+ if (IPAddress.isValid(invalid[i]))
+ {
+ fail("Invalid input string accepted: " + invalid[i] + ".");
+ }
+ }
+ }
+
+ public String getName()
+ {
+ return "IPTest";
+ }
+
+ public void testIPv4()
+ {
+ testIP(validIP4v, invalidIP4v);
+ }
+
+ public void testIPv6()
+ {
+ testIP(validIP6v, invalidIP6v);
+ }
+}