1 package org.objectweb.celtix.common.util; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.StringWriter ; 5 6 import junit.framework.TestCase; 7 8 public class Base64UtilityTest extends TestCase { 9 10 public Base64UtilityTest(String arg0) { 11 super(arg0); 12 } 13 14 void assertEquals(byte b1[], byte b2[]) { 15 assertEquals(b1.length, b2.length); 16 for (int x = 0; x < b1.length; x++) { 17 assertEquals(b1[x], b2[x]); 18 } 19 } 20 21 public void testEncodeDecodeChunk() throws Exception { 22 byte bytes[] = new byte[100]; 23 for (int x = 0; x < bytes.length; x++) { 24 bytes[x] = (byte)x; 25 } 26 27 char encodedChars[] = Base64Utility.encodeChunk(bytes, 0, -2); 28 assertNull(encodedChars); 29 encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length); 30 assertNotNull(encodedChars); 31 byte bytesDecoded[] = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length); 32 assertEquals(bytes, bytesDecoded); 33 34 bytes = new byte[99]; 36 for (int x = 0; x < bytes.length; x++) { 37 bytes[x] = (byte)x; 38 } 39 encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length); 40 assertNotNull(encodedChars); 41 bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length); 42 assertEquals(bytes, bytesDecoded); 43 44 bytes = new byte[98]; 46 for (int x = 0; x < bytes.length; x++) { 47 bytes[x] = (byte)x; 48 } 49 encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length); 50 assertNotNull(encodedChars); 51 bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length); 52 assertEquals(bytes, bytesDecoded); 53 54 bytes = new byte[97]; 56 for (int x = 0; x < bytes.length; x++) { 57 bytes[x] = (byte)x; 58 } 59 encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length); 60 assertNotNull(encodedChars); 61 bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length); 62 assertEquals(bytes, bytesDecoded); 63 64 65 bytesDecoded = Base64Utility.decodeChunk(new char[3], 0, 3); 66 assertNull(bytesDecoded); 67 bytesDecoded = Base64Utility.decodeChunk(new char[9], 0, 9); 68 assertNull(bytesDecoded); 69 } 70 71 public void testEncodeDecodeString() throws Exception { 72 String in = "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; 73 byte bytes[] = Base64Utility.decode(in); 74 assertEquals("Aladdin:open sesame", new String (bytes)); 75 String encoded = Base64Utility.encode(bytes); 76 assertEquals(in, encoded); 77 } 78 79 public void testEncodeDecodeStreams() throws Exception { 80 byte bytes[] = new byte[100]; 81 for (int x = 0; x < bytes.length; x++) { 82 bytes[x] = (byte)x; 83 } 84 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 85 ByteArrayOutputStream bout2 = new ByteArrayOutputStream (); 86 Base64Utility.encodeChunk(bytes, 0, bytes.length, bout); 87 String encodedString = new String (bout.toByteArray()); 88 Base64Utility.decode(encodedString.toCharArray(), 89 0, 90 encodedString.length(), 91 bout2); 92 assertEquals(bytes, bout2.toByteArray()); 93 94 95 String in = "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; 96 bout.reset(); 97 bout2.reset(); 98 Base64Utility.decode(in, bout); 99 bytes = bout.toByteArray(); 100 assertEquals("Aladdin:open sesame", new String (bytes)); 101 StringWriter writer = new StringWriter (); 102 Base64Utility.encode(bytes, 0, bytes.length, writer); 103 assertEquals(in, writer.toString()); 104 105 } 106 107 108 } 109 | Popular Tags |