1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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
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 }