001: package org.objectweb.celtix.common.util;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.StringWriter;
005:
006: import junit.framework.TestCase;
007:
008: public class Base64UtilityTest extends TestCase {
009:
010: public Base64UtilityTest(String arg0) {
011: super (arg0);
012: }
013:
014: void assertEquals(byte b1[], byte b2[]) {
015: assertEquals(b1.length, b2.length);
016: for (int x = 0; x < b1.length; x++) {
017: assertEquals(b1[x], b2[x]);
018: }
019: }
020:
021: public void testEncodeDecodeChunk() throws Exception {
022: byte bytes[] = new byte[100];
023: for (int x = 0; x < bytes.length; x++) {
024: bytes[x] = (byte) x;
025: }
026:
027: char encodedChars[] = Base64Utility.encodeChunk(bytes, 0, -2);
028: assertNull(encodedChars);
029: encodedChars = Base64Utility
030: .encodeChunk(bytes, 0, bytes.length);
031: assertNotNull(encodedChars);
032: byte bytesDecoded[] = Base64Utility.decodeChunk(encodedChars,
033: 0, encodedChars.length);
034: assertEquals(bytes, bytesDecoded);
035:
036: //require padding
037: bytes = new byte[99];
038: for (int x = 0; x < bytes.length; x++) {
039: bytes[x] = (byte) x;
040: }
041: encodedChars = Base64Utility
042: .encodeChunk(bytes, 0, bytes.length);
043: assertNotNull(encodedChars);
044: bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0,
045: encodedChars.length);
046: assertEquals(bytes, bytesDecoded);
047:
048: //require padding
049: bytes = new byte[98];
050: for (int x = 0; x < bytes.length; x++) {
051: bytes[x] = (byte) x;
052: }
053: encodedChars = Base64Utility
054: .encodeChunk(bytes, 0, bytes.length);
055: assertNotNull(encodedChars);
056: bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0,
057: encodedChars.length);
058: assertEquals(bytes, bytesDecoded);
059:
060: //require padding
061: bytes = new byte[97];
062: for (int x = 0; x < bytes.length; x++) {
063: bytes[x] = (byte) x;
064: }
065: encodedChars = Base64Utility
066: .encodeChunk(bytes, 0, bytes.length);
067: assertNotNull(encodedChars);
068: bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0,
069: encodedChars.length);
070: assertEquals(bytes, bytesDecoded);
071:
072: bytesDecoded = Base64Utility.decodeChunk(new char[3], 0, 3);
073: assertNull(bytesDecoded);
074: bytesDecoded = Base64Utility.decodeChunk(new char[9], 0, 9);
075: assertNull(bytesDecoded);
076: }
077:
078: public void testEncodeDecodeString() throws Exception {
079: String in = "QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
080: byte bytes[] = Base64Utility.decode(in);
081: assertEquals("Aladdin:open sesame", new String(bytes));
082: String encoded = Base64Utility.encode(bytes);
083: assertEquals(in, encoded);
084: }
085:
086: public void testEncodeDecodeStreams() throws Exception {
087: byte bytes[] = new byte[100];
088: for (int x = 0; x < bytes.length; x++) {
089: bytes[x] = (byte) x;
090: }
091: ByteArrayOutputStream bout = new ByteArrayOutputStream();
092: ByteArrayOutputStream bout2 = new ByteArrayOutputStream();
093: Base64Utility.encodeChunk(bytes, 0, bytes.length, bout);
094: String encodedString = new String(bout.toByteArray());
095: Base64Utility.decode(encodedString.toCharArray(), 0,
096: encodedString.length(), bout2);
097: assertEquals(bytes, bout2.toByteArray());
098:
099: String in = "QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
100: bout.reset();
101: bout2.reset();
102: Base64Utility.decode(in, bout);
103: bytes = bout.toByteArray();
104: assertEquals("Aladdin:open sesame", new String(bytes));
105: StringWriter writer = new StringWriter();
106: Base64Utility.encode(bytes, 0, bytes.length, writer);
107: assertEquals(in, writer.toString());
108:
109: }
110:
111: }
|