1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: Base64CoderTest.java 1605 2008-02-25 21:07:14Z barteo $
26   */
27  package org.microemu.util;
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * @author vlads
33   * @author <a href="mailto:russgold@acm.org">Russell Gold</a>
34   * @author <a href="mailto:mtarruella@silacom.com">Marcos Tarruella</a> 
35   *
36   */
37  public class Base64CoderTest extends TestCase {
38  
39  
40      public void testEncode() {
41          assertEquals( "Result of encoding", "QWxhZGRpbjpvcGVuIHNlc2FtZQ==", Base64Coder.encode( "Aladdin:open sesame" ) );
42          assertEquals( "Result of encoding", "QWRtaW46Zm9vYmFy",             Base64Coder.encode( "Admin:foobar" ) );
43      }
44  
45  
46      public void testDecode() {
47          assertEquals( "Result of decoding", "Aladdin:open sesame", Base64Coder.decode( "QWxhZGRpbjpvcGVuIHNlc2FtZQ==" ) );
48          assertEquals( "Result of decoding", "Admin:foobar",        Base64Coder.decode( "QWRtaW46Zm9vYmFy" ) );
49      }
50  
51  	private void verifyEncodeDecode(byte[] value) {
52  		char[] chars = Base64Coder.encode(value);
53  		byte[] decodedValue = Base64Coder.decode(chars);
54  		assertEquals("Data as expected length", value.length, decodedValue.length);
55  		for(int i = 0; i < value.length; i++ ) {
56  			assertEquals("Data as expected", value[i], decodedValue[i]);	
57  		}
58  	}
59  	
60  	public void testAllBytes() {
61  		final String message = "Wrong number...";
62  		int len = message.getBytes().length;
63  		byte[] data = new byte[len + 256];
64  		byte b = -127;
65  		for(int i = len; i < data.length; i++ ) {
66  			data[i] = b;
67  			b ++;
68  		}
69  		verifyEncodeDecode(data);
70  	}
71  }