1 36 package org.columba.ristretto.coder; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.nio.ByteBuffer ; 42 import java.util.Random ; 43 44 import junit.framework.TestCase; 45 46 public class Base64EncoderInputStreamTest extends TestCase { 47 48 52 public Base64EncoderInputStreamTest(String arg0) { 53 super(arg0); 54 } 55 56 public void test0Pads() { 57 byte[] input = {(byte)0x0ff, (byte)0x080, (byte)0x040,(byte)0x0ff, (byte)0x080, (byte)0x040}; 58 String result = "/4BA/4BA"; 59 InputStream in = new Base64EncoderInputStream( new ByteArrayInputStream ( input )); 60 int pos = 0; 61 62 try { 63 int next = in.read(); 64 while( next != -1 ) { 65 assertTrue( next == result.charAt(pos++)); 66 next = in.read(); 67 } 68 } catch (IOException e) { 69 e.printStackTrace(); 70 } 71 72 assertTrue( pos == 8); 73 } 74 75 public void test1Pads() { 76 byte[] input = {(byte)0x0ff, (byte)0x080, (byte)0x040,(byte)0x0ff, (byte)0x080 }; 77 String result = "/4BA/4A="; 78 InputStream in = new Base64EncoderInputStream( new ByteArrayInputStream ( input )); 79 int pos = 0; 80 81 try { 82 int next = in.read(); 83 while( next != -1 ) { 84 assertTrue( next == result.charAt(pos++)); 85 next = in.read(); 86 } 87 } catch (IOException e) { 88 e.printStackTrace(); 89 } 90 91 assertTrue( pos == 8); 92 } 93 94 95 public void test2Pads() { 96 byte[] input = {(byte)0x0ff, (byte)0x080, (byte)0x040,(byte)0x0ff }; 97 String result = "/4BA/w=="; 98 InputStream in = new Base64EncoderInputStream( new ByteArrayInputStream ( input )); 99 int pos = 0; 100 101 try { 102 int next = in.read(); 103 while( next != -1 ) { 104 assertTrue( next == result.charAt(pos++)); 105 next = in.read(); 106 } 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } 110 111 assertTrue( pos == 8); 112 } 113 114 public void testEncodeDecode() { 115 Random random = new Random (); 116 byte[] testInput = new byte[(int) (random.nextFloat() * 1024)]; 117 random.nextBytes(testInput); 118 StringBuffer encoded = new StringBuffer (); 119 InputStream in = new Base64EncoderInputStream( new ByteArrayInputStream (testInput)); 120 try { 121 int next = in.read(); 122 while( next != -1 ) { 123 encoded.append((char) next); 124 next = in.read(); 125 } 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } 129 System.out.println( encoded ); 130 ByteBuffer decoded = Base64.decode(encoded); 131 for( int i=0; i<testInput.length; i++) { 132 int next = decoded.get(); 133 if(testInput[i] != next ) { 134 System.out.println( i ); 135 } 136 assertTrue(testInput[i] == next); 137 } 138 } 139 } 140 | Popular Tags |