BasicConfigurator.configure();
}
public void testDecode() throws IOException {
ByteArrayInputStream bis = null;
Base64InputStream decoder = null;
byte[] bytes = null;
/*
* Simple initial test.
*/
bis = new ByteArrayInputStream(
fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ=="));
decoder = new Base64InputStream(bis);
assertEquals("This is the plain text message!", toString(read(decoder)));
/*
* Test encoded text padded once, twice and not at all.
*/
bis = new ByteArrayInputStream(
fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl"
+ "IHBhZGRlZCBvbmNlLi4="));
decoder = new Base64InputStream(bis);
assertEquals("This is a text which has to be padded once..", toString(read(decoder)));
bis = new ByteArrayInputStream(
fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl"
+ "IHBhZGRlZCB0d2ljZQ=="));
decoder = new Base64InputStream(bis);
assertEquals("This is a text which has to be padded twice", toString(read(decoder)));
bis = new ByteArrayInputStream(
fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggd2lsbCBub3Qg"
+ "YmUgcGFkZGVk"));
decoder = new Base64InputStream(bis);
assertEquals("This is a text which will not be padded", toString(read(decoder)));
/*
* Test that non base64 characters are ignored.
*/
bis = new ByteArrayInputStream(
fromString(" &% VGhp\r\ncyBp\r\ncyB0aGUgcGxhaW4g "
+ " \tdGV4dCBtZ?!XNzY*WdlIQ=="));
decoder = new Base64InputStream(bis);
assertEquals("This is the plain text message!", toString(read(decoder)));
/*
* Test that the bytes 0-255 shifted 0, 1 and 2 positions are
* decoded properly.
*/
String s1 = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCU"
+ "mJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0"
+ "xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bxc"
+ "nN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeY"
+ "mZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6"
+ "/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5O"
+ "Xm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
String s2 = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSY"
+ "nKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE"
+ "1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc"
+ "3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZ"
+ "mpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/"
+ "AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5e"
+ "bn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AA==";
String s3 = "AgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJic"
+ "oKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU"
+ "5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzd"
+ "HV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJma"
+ "m5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8D"
+ "BwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5u"
+ "fo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQ==";
bis = new ByteArrayInputStream(fromString(s1));
decoder = new Base64InputStream(bis);
bytes = read(decoder);
for (int i = 0; i < bytes.length; i++) {
assertEquals("Position " + i, bytes[i], (byte) i);
}
bis = new ByteArrayInputStream(fromString(s2));
decoder = new Base64InputStream(bis);
bytes = read(decoder);
for (int i = 0; i < bytes.length; i++) {
assertEquals("Position " + i, bytes[i], (byte) (i + 1));
}
bis = new ByteArrayInputStream(fromString(s3));
decoder = new Base64InputStream(bis);
bytes = read(decoder);
for (int i = 0; i < bytes.length; i++) {
assertEquals("Position " + i, bytes[i], (byte) (i + 2));
}