1 /** 2 * Copyright (c) 2008, http://www.snakeyaml.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package biz.source_code.base64Coder; 17 18 import java.io.UnsupportedEncodingException; 19 20 import junit.framework.TestCase; 21 22 import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; 23 24 public class Base64CoderTest extends TestCase { 25 testDecode()26 public void testDecode() throws UnsupportedEncodingException { 27 check("Aladdin:open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); 28 check("a", "YQ=="); 29 check("aa", "YWE="); 30 check("a=", "YT0="); 31 check("", ""); 32 } 33 testFailure1()34 public void testFailure1() throws UnsupportedEncodingException { 35 try { 36 Base64Coder.decode("YQ=".toCharArray()); 37 fail(); 38 } catch (Exception e) { 39 assertEquals("Length of Base64 encoded input string is not a multiple of 4.", 40 e.getMessage()); 41 } 42 } 43 testFailure2()44 public void testFailure2() throws UnsupportedEncodingException { 45 checkInvalid("\tWE="); 46 checkInvalid("Y\tE="); 47 checkInvalid("YW\t="); 48 checkInvalid("YWE\t"); 49 // 50 checkInvalid("©WE="); 51 checkInvalid("Y©E="); 52 checkInvalid("YW©="); 53 checkInvalid("YWE©"); 54 } 55 checkInvalid(String encoded)56 private void checkInvalid(String encoded) { 57 try { 58 Base64Coder.decode(encoded.toCharArray()); 59 fail("Illegal chanracter."); 60 } catch (Exception e) { 61 assertEquals("Illegal character in Base64 encoded data.", e.getMessage()); 62 } 63 } 64 check(String text, String encoded)65 private void check(String text, String encoded) throws UnsupportedEncodingException { 66 char[] s1 = Base64Coder.encode(text.getBytes("UTF-8")); 67 String t1 = new String(s1); 68 assertEquals(encoded, t1); 69 byte[] s2 = Base64Coder.decode(encoded.toCharArray()); 70 String t2 = new String(s2, "UTF-8"); 71 assertEquals(text, t2); 72 } 73 } 74