KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > coder > Base64Test


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.coder;
37
38 import java.nio.ByteBuffer JavaDoc;
39 import java.util.Random JavaDoc;
40
41 import junit.framework.TestCase;
42
43 public class Base64Test extends TestCase {
44
45     /**
46      * Constructor for Base64Test.
47      * @param arg0
48      */

49     public Base64Test(String JavaDoc arg0) {
50         super(arg0);
51     }
52
53     public void testDecode1Pack0Pads() {
54         String JavaDoc input = "/4BA";
55         ByteBuffer JavaDoc decoded = Base64.decode(input);
56         
57         assertTrue( decoded.capacity()== 3);
58         assertTrue( decoded.get() == (byte) 0x0ff );
59         assertTrue( decoded.get()== (byte) 0x080 );
60         assertTrue( decoded.get() == (byte) 0x040 );
61     }
62
63     public void testDecode2Pack0Pads() {
64         String JavaDoc input = "/4BA/4BA";
65         ByteBuffer JavaDoc decoded = Base64.decode(input);
66         
67         assertTrue( decoded.capacity() == 6);
68         assertTrue( decoded.get() == (byte) 0x0ff );
69         assertTrue( decoded.get() == (byte) 0x080 );
70         assertTrue( decoded.get() == (byte) 0x040 );
71         assertTrue( decoded.get() == (byte) 0x0ff );
72         assertTrue( decoded.get() == (byte) 0x080 );
73         assertTrue( decoded.get() == (byte) 0x040 );
74     }
75
76     public void testDecode1Pack1Pads() {
77         String JavaDoc input = "/4A=";
78         ByteBuffer JavaDoc decoded = Base64.decode(input);
79         
80         assertTrue( decoded.limit() == 2);
81         assertTrue( decoded.get() == (byte) 0x0ff );
82         assertTrue( decoded.get() == (byte) 0x080 );
83     }
84
85     public void testDecode1Pack2Pads() {
86         String JavaDoc input = "/w==";
87         ByteBuffer JavaDoc decoded = Base64.decode(input);
88         
89         assertTrue( decoded.limit() == 1);
90         assertTrue( decoded.get() == (byte) 0x0ff );
91     }
92
93     public void testDecode1Pack2PadsAndGarbageAtEnd() {
94         String JavaDoc input = "/4== asldkfie sdhr oi";
95         ByteBuffer JavaDoc decoded = Base64.decode(input);
96         
97         assertTrue( decoded.limit() == 1);
98         assertTrue( decoded.get() == (byte) 0x0ff );
99     }
100     
101     public void testEncode1Pack0Pads() {
102         byte[] input = {(byte)0x0ff, (byte)0x080, (byte)0x040};
103         StringBuffer JavaDoc result = Base64.encode(ByteBuffer.wrap(input));
104         assertTrue(result.length() == 4);
105         assertTrue(result.toString().equals("/4BA"));
106     }
107
108     public void testEncode2Pack0Pads() {
109         byte[] input = {(byte)0x0ff, (byte)0x080, (byte)0x040,(byte)0x0ff, (byte)0x080, (byte)0x040};
110         StringBuffer JavaDoc result = Base64.encode(ByteBuffer.wrap(input));
111         assertTrue(result.length() == 8);
112         assertTrue(result.toString().equals("/4BA/4BA"));
113     }
114
115     public void testEncode1Pack1Pads() {
116         byte[] input = {(byte)0x0ff, (byte)0x080};
117         StringBuffer JavaDoc result = Base64.encode(ByteBuffer.wrap(input));
118         assertTrue(result.length() == 4);
119         assertTrue(result.toString().equals("/4A="));
120     }
121
122     public void testEncode1Pack2Pads() {
123         byte[] input = {(byte)0x0ff};
124         StringBuffer JavaDoc result = Base64.encode(ByteBuffer.wrap(input));
125         assertTrue(result.length() == 4);
126         assertTrue(result.toString().equals("/w=="));
127     }
128     
129     public void testEncodeDecode() {
130         Random JavaDoc random = new Random JavaDoc();
131         //byte[] testInput = new byte[(int) (random.nextFloat() * 1024)];
132
byte[] testInput = new byte[91];
133         random.nextBytes(testInput);
134         StringBuffer JavaDoc encoded = Base64.encode(ByteBuffer.wrap(testInput));
135         System.out.println( encoded );
136         ByteBuffer JavaDoc decoded = Base64.decode(encoded);
137         for( int i=0; i<testInput.length; i++) {
138             assertEquals(testInput[i],decoded.get());
139         }
140     }
141     
142     public void testDecodeMalformed() {
143         String JavaDoc input = "tqEgx9G55r/vILHuwfYgu/27/cfPsNQgwPzH2LXluLO0z7TZLiC18Lryvbogv7XIrcDHIMPWsO26wCEh=";
144         
145         ByteBuffer JavaDoc decoded = Base64.decode(input);
146     }
147     
148 }
149
Popular Tags